package com.ffcs.ssh.web.login;

import org.springframework.beans.factory.annotation.Autowired;

import com.ffcs.ssh.common.web.StrutsAction;
import com.ffcs.ssh.entity.base.User;
import com.ffcs.ssh.service.base.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends StrutsAction implements Preparable{

	private String username; //登录用户名
	private String password; //登录密码
	
	@Autowired
	private UserManager userManager; 
	
	/**
	 * 进入登陆页面login.jsp 
	 */
	public String welcome(){
		return SUCCESS;
	}
	
	/**
	public String list(){
		return SUCCESS;
	}
	*/
	
	/**
	 * 这个用来干嘛?
	 */
	public String loginRedirect(){
		return "index";
	}
	
	/**
	 * 登陆认证 
	 */
	public String login(){
		User user = userManager.getUser(username, password);
		if (user == null) {
			addActionError("登录名或密码不正确!");
			return SUCCESS;
			//return Action.LOGIN;
		}
		ActionContext.getContext().getSession().put("user", user);
		return "index";
	}
	
	public String logout(){
		ActionContext.getContext().getSession().put("user", null);
		return SUCCESS;
	}
	
	/**
	 * 防止重复提示错误信息
	 */
	public void prepare() {
		clearErrorsAndMessages();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
